
package UTILITY;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class KetNoiDuLieu {
    
           public  Connection kn;
             Statement st;
             String userName = "root";
             String password = "123";
             String url = "jdbc:mysql://127.0.0.1:3306/gvsv";
             
            public  Statement getStatement()
            {
               
                try {
                        
                        Class.forName ("com.mysql.jdbc.Driver").newInstance ();
                        //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        kn = DriverManager.getConnection (url,userName, password);
                        System.out.println ("Da ket noi CSDL");
                        st=kn.createStatement();
                        return st;
                    /*
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");// Load driver
                    kn=DriverManager.getConnection("jdbc:odbc:gvsv"); // tao statament
                    st=kn.createStatement();// tao statament
                    return st;*/
                } 
                catch (Exception ex) {
                            Logger.getLogger(KetNoiDuLieu.class.getName()).log(Level.SEVERE, null, ex);
                            return null;
                 }
            }
            
            public void closeStatement( Statement a)
            {
                try {
                    a.close();
                } catch (SQLException ex) {
                    Logger.getLogger(KetNoiDuLieu.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        public void open(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch(ClassNotFoundException ex){
            System.out.println(ex.getMessage());
        }
        try{
            kn=DriverManager.getConnection(url,userName,password);
            kn.setAutoCommit(false);
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }
              public void close(){
        try{
            kn.close();
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }
             
   public ResultSet executeQuery(String sql){
        ResultSet rs=null;
        try{
            Statement st=this.kn.createStatement();
            rs=st.executeQuery(sql);
           
            
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
        return rs;
    }
    public int executeUpdate(String sql){
        int n=-1;
        try{
            //KetNoiDuLieu kn=new KetNoiDuLieu();
           Statement st=this.kn.createStatement();  
            n=st.executeUpdate(sql);
            st.close();
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
        return n;
    }
            
            
                
}
